//Copyright 2010 Balazs Keri, Imre Keri
//
//This file is part of FireworksCreator.
//
//FireworksCreator is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.
//
//FireworksCreator is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.
//
//You should have received a copy of the GNU General Public License
//along with FireworksCreator.  If not, see <http://www.gnu.org/licenses/>.
//
#include "osgviewerwidget.h"
#include "qttoosgkeycodetranslator.h"
#include <QtGui/QKeyEvent>
#include <QtGui/QMouseEvent>
//
OSGViewerWidget::OSGViewerWidget(QWidget *p_parent,const char *p_name,const QGLWidget *p_share_widget,Qt::WindowFlags p_f)
:
   QGLWidget(p_parent,p_share_widget,p_f)
{
   setObjectName(p_name);
   setFocusPolicy(Qt::ClickFocus);
   graphics_window=new osgViewer::GraphicsWindowEmbedded(0,0,width(),height());
   viewer=new osgViewer::Viewer();
   viewer->setThreadingModel(osgViewer::Viewer::SingleThreaded);
   viewer->getCamera()->setViewport(new osg::Viewport(0,0,width(),height()));
   viewer->getCamera()->setGraphicsContext(graphics_window);
   connect(&update_timer,SIGNAL(timeout()),this,SLOT(updateGL()));
   update_timer.start(10);
}
OSGViewerWidget::~OSGViewerWidget()
{
}
//
osgViewer::GraphicsWindow *OSGViewerWidget::get_graphics_window()
{
   return graphics_window;
}
const osgViewer::GraphicsWindow *OSGViewerWidget::get_graphics_window()const
{
   return graphics_window;
}
osgViewer::Viewer *OSGViewerWidget::get_viewer()
{
   return viewer;
}
const osgViewer::Viewer *OSGViewerWidget::get_viewer()const
{
   return viewer;
}
//
void OSGViewerWidget::resizeGL(int width,int height)
{
   //viewer->getCamera()->setViewport(new osg::Viewport(0,0,width,height));
   graphics_window->getEventQueue()->windowResize(0,0,width,height);
   graphics_window->resized(0,0,width,height);
   viewer->getCamera()->setProjectionMatrixAsPerspective(30.0f, static_cast<double>(width)/static_cast<double>(height), 1.0f, 10000.0f);
}
void OSGViewerWidget::paintGL()
{
   viewer->frame();
}
void OSGViewerWidget::keyPressEvent(QKeyEvent *event)
{
   if(!event->text().isEmpty())
   {
      QByteArray data=event->text().toLatin1().data();
      for(int i=0;i<data.length();++i)
         graphics_window->getEventQueue()->keyPress((osgGA::GUIEventAdapter::KeySymbol)data.at(i));
   }
   else
   {
      int code; bool ok;
      QtToOsgKeyCodeTranslator::qt_key_to_osg_key(event->key(),code,ok);
      if(ok)
         graphics_window->getEventQueue()->keyPress((osgGA::GUIEventAdapter::KeySymbol)code);
      else
         qDebug()<<"failed to translate key code to osg";
   }
}
void OSGViewerWidget::keyReleaseEvent(QKeyEvent *event)
{
   /*
   int code=0; bool ok=true;
   if(!event->text().isEmpty())
      code=*(event->text().toAscii().data());
   else
      QtToOsgKeyCodeTranslator::qt_key_to_osg_key(event->key(),code,ok);
   if(ok)
      graphics_window->getEventQueue()->keyRelease((osgGA::GUIEventAdapter::KeySymbol)code);
   */
   if(!event->text().isEmpty())
   {
      QByteArray data=event->text().toLatin1().data();
      for(int i=0;i<data.length();++i)
         graphics_window->getEventQueue()->keyRelease((osgGA::GUIEventAdapter::KeySymbol)data.at(i));
   }
   else
   {
      int code; bool ok;
      QtToOsgKeyCodeTranslator::qt_key_to_osg_key(event->key(),code,ok);
      if(ok)
         graphics_window->getEventQueue()->keyRelease((osgGA::GUIEventAdapter::KeySymbol)code);
      else
         qDebug()<<"failed to translate key code to osg";
   }
}
void OSGViewerWidget::mousePressEvent(QMouseEvent *event)
{
   int button=0;
   switch(event->button())
   {
   case Qt::LeftButton:
      button=1;
      break;
   case Qt::MidButton:
      button=2;
      break;
   case Qt::RightButton:
      button=3;
      break;
   default:
      button=0;
      break;
   }
   graphics_window->getEventQueue()->mouseButtonPress(event->x(),event->y(),button);
}
void OSGViewerWidget::mouseReleaseEvent(QMouseEvent *event)
{
   int button=0;
   switch(event->button())
   {
   case Qt::LeftButton:
      button=1;
      break;
   case Qt::MidButton:
      button=2;
      break;
   case Qt::RightButton:
      button=3;
      break;
   default:
      button=0;
      break;
   }
   graphics_window->getEventQueue()->mouseButtonRelease(event->x(),event->y(),button);
}
void OSGViewerWidget::mouseMoveEvent(QMouseEvent *event)
{
   graphics_window->getEventQueue()->mouseMotion(event->x(),event->y());
}
void OSGViewerWidget::wheelEvent(QWheelEvent *event)
{
   graphics_window->getEventQueue()->mouseScroll((event->delta()>0)?osgGA::GUIEventAdapter::SCROLL_UP:osgGA::GUIEventAdapter::SCROLL_DOWN);
}
